import { _decorator, Component, Label, log, Node, sp, Sprite } from 'cc';
const { ccclass, property } = _decorator;
//这个是回血
@ccclass('regainHP')
export class regainHP extends Component {
   @property(Label)
    countdownLabel: Label = null!;
    
    @property
    totalTime: number = 5; // 总时间5秒
    
    private currentTime: number = 0;
    public isCounting: boolean = false;
    
    onLoad() {
        this.resetCountdown();
    }
    
    // 开始倒计时
    startCountdown() {
        this.isCounting = true;
        this.currentTime = this.totalTime;
        this.updateDisplay();
        this.countdownLabel.node.active = true;
        this.node.active = true;
    }
    
    // 重置倒计时
    resetCountdown() {
        this.isCounting = false;
        this.currentTime = this.totalTime;
        this.updateDisplay();
        this.countdownLabel.node.active = false;
        this.node.active = false;
    }
    
    update(deltaTime: number) {
        if (!this.isCounting) return;
        
        this.currentTime -= deltaTime;
        
        if (this.currentTime <= 0) {
            this.currentTime = 0;
            this.isCounting = false;
            this.onCountdownComplete();
        }
        
        this.updateDisplay();
    }
    
    updateDisplay() {
        if (this.countdownLabel) {
            // 显示保留1位小数
            this.countdownLabel.string = this.currentTime.toFixed(1);
            this.getComponent(Sprite).fillRange = 1 - this.getProgress();
            // 或者显示整数（四舍五入）
            // this.countdownLabel.string = Math.round(this.currentTime).toString();
        }
    }
    
    onCountdownComplete() {
        console.log("倒计时结束！");
         this.resetCountdown();
        // 这里可以触发倒计时结束的事件
        // this.node.emit('countdown-complete');
    }
    
    // 获取当前进度（0-1）
    getProgress(): number {
        return 1 - (this.currentTime / this.totalTime);
    }
    
    // 获取剩余时间
    getRemainingTime(): number {
        return this.currentTime;
    }
}


